{% init item_list = [] %}
{% init show_todo_check = False %}
{% init show_edit_btn   = True %}
{% init show_to_log_btn = False %}

{% if len(item_list) == 0 %}
    <div class="card">
        {% include common/text/empty_text.html %}
    </div>
{% end %}

{% for item in item_list %}
    <div class="card">
        <div class="todo-row">
            <!-- <div class="todo-user">${user}</div> -->
            <div class="col-md-12">
                <div class="todo-time">{{item.ctime}}</div>

                {% if item.tag == "task" %}
                    <span class="task-tag">{{item.tag_text}}</span>
                {% else %}
                    <span class="todo-tag">{{item.tag_text}}</span>
                {% end %}

                <div class="float-right">
                    {% if item.tag == "task" %}
                        <!-- <button class="dialog-btn btn-default">编辑</button> -->
                        <input type="checkbox" data-id="{{item.id}}" class="todo-checkbox" onclick="onMessageDone(this)">
                    {% end %}

                    {% if item.tag == "done1" %}
                        <a class="update-tag-btn btn btn-default" data-id="{{item.id}}" data-tag="task">重做</a>
                    {% end %}
                </div>
            </div>
            <div class="todo-content">{% raw item.html %}</div>
            <div class="todo-foot">
                
                {% if show_edit_btn %}
                    <a class="todo-edit-btn btn btn-default" 
                        data-url="/message/edit?id={{item.id}}">编辑</a>
                {% end %}

                <div class="float-right">
                    <a class="todo-delete-btn btn btn-default" data-id="{{item.id}}" data-content="{{item.content}}">删除</a>
                </div>
            </div>
        </div>
    </div>
{% end %}

<div class="hide edit-option-dialog">
    <div class="list-item">
        <span>完成任务</span>
        <div class="float-right">
            <i class="fa fa-chevron-right"></i>
        </div>
    </div>
    
    <div class="list-item">
        <span class="red">删除任务</span>
        <div class="float-right">
            <i class="fa fa-chevron-right"></i>
        </div>
    </div>
</div>


<script type="text/javascript">
$(function () {

    // 编辑弹窗
    $(".todo-edit-btn").click(function (e) {
        var url = $(e.target).attr("data-url");
        var layerId = xnote.showIframeDialog("编辑", url);
        // 更新后会调用 postMessage 发送消息
    });

    function updateMessageTag(id, tag) {
        $.post("/message/tag", {id: id, tag: tag}, function (resp) {
            if (resp.code == "success") {
                xnote.fire("message.updated");
            } else {
                alert(resp.message);
            }
        });
    }

    $(".update-tag-btn").click(function (e) {
        var id = $(e.target).attr("data-id");
        var tag = $(e.target).attr("data-tag");
        console.log(id, tag);

        updateMessageTag(id, tag);
    });

    $(".todo-delete-btn").click(function (e) {
        var id = $(e.target).attr("data-id");
        var content = $(e.target).attr("data-content");

        xnote.confirm("确认删除[" + content + "]吗?", function (result) {
            $.post("/message/delete", {id: id}, function (resp) {
                if (resp.code == "success") {
                    var msg = {type: "message.updated"};
                    window.postMessage(JSON.stringify(msg), "*");
                } else {
                    xnote.alert(data.message);
                }
            });
        });
    });

    window.onMessageDone = function(target) {
        // todo完成的声音
        xnote.playAudio("todo_done");
        xnote.toast("更新任务状态成功");

        // 标记为完成
        var id = $(target).attr("data-id");

        updateMessageTag(id, "done");
    }
    
    // 加载音频文件
    xnote.loadAudio("todo_done", "/static/audio/todo_done.mp3");
});
</script>

<div class="card">
    {% include common/pagination.html %}
</div>